package com.example.demo_mysql_mybatisplus.configs;

import liquibase.integration.spring.SpringLiquibase;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.LinkedHashMap;

@Configuration
@ConditionalOnClass(DataSource.class)
public class LiquibaseConfig {

    private final DataSource dataSource;

    public LiquibaseConfig(DataSource dataSource) {
        this.dataSource = dataSource;
    }

    @Value("${liquibase-enable:true}")
    private boolean enable=true;

    @Value("${customized.liquibase.additionalPath_1}")
    private String additionalPath_1;

    @Bean
    SpringLiquibase liquibase() {

        SpringLiquibase liquibase = new SpringLiquibase();
        liquibase.setShouldRun(enable);
        liquibase.setDataSource(dataSource);
        liquibase.setChangeLog( "classpath:liquibase/master.xml" );

        // 动态的 额外 扫描配置
        liquibase.setChangeLogParameters( new LinkedHashMap(){{
            put("additionalPath_1",additionalPath_1);
        }});

        return liquibase;
    }
}